package com.kim.bookshop.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import com.kim.bookshop.user.bean.User;

@WebFilter("/admin/*")
public class AdminFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub
	}


	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpServletRequest=(HttpServletRequest) request;
		User admin=(User) httpServletRequest.getSession().getAttribute("admin");
		if(admin!=null){
			chain.doFilter(request, response);
		}else {
			httpServletRequest.setAttribute("msg", "请已管理员身份登录后访问");
			httpServletRequest.getRequestDispatcher("/AdminLogin.jsp").forward(httpServletRequest, response);
		}
		
	}


	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}
